import {
  selectAll,
  add,
  del
} from '@/api/roleMenu'

const getDefaultState = () => {
  return {

  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  // use select all role menu
  selectAll({
    commit
  }, roleMenuInfo) {
    return new Promise((resolve, reject) => {
      const {
        roleId,
        name,
        page,
        pageSize
      } = roleMenuInfo
      selectAll({
        roleId: roleId,
        name: name,
        page: page,
        pageSize: pageSize
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use add
  add({
    commit
  }, roleMenuInfo) {
    return new Promise((resolve, reject) => {
      const {
        roleId,
        menuId
      } = roleMenuInfo
      add({
        roleId: roleId,
        menuId: menuId
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use del
  del({
    commit
  }, roleMenuInfo) {
    return new Promise((resolve, reject) => {
      const {
        menuRoleId
      } = roleMenuInfo
      del({
        menuRoleId: menuRoleId
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
